home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / MATH / CIS610 / CIS610.PAS
Pascal/Delphi Source File  |  1994-07-17  |  18KB  |  676 lines

  1. program CIS610_PROJECT;
  2. {***********************************************************************}
  3.  
  4. {.$I pr1file.txt}                  {programmer's test file }
  5.  
  6.  
  7.  
  8. uses
  9.   WinCrt;  { Allows Writeln, Readln, cursor movement, etc. }
  10.   
  11. { This program was written by Larry Marks, Student ID # 066-50-9637, for
  12. Professor Oscar Yang for CIS 610, Data Structures and Programming on July
  13. 10, 1994 to test the learning of the ADT and linked list technique. This
  14. program is due on 8/3/94. This program is written in BORLAND'S PASCAL.
  15.  
  16.  
  17. OBJECTIVE
  18.  
  19. The purpose of this program is to design and implement a computational
  20. system for polynomials with various mathematical operations.
  21.  
  22. A polynomial is represented as a linked list of pairs containing an exponent and
  23. a coefficient. In other words, the input for example, 2x^3 - 1 would be
  24. evaluated as "2x^3 + 0x^2 + 0x^1 - 1".
  25.  
  26. This program is designed to perform the following:
  27.  
  28. 1)READ a polynomial P.  This will be read from an input file using the
  29. recommended "list of pairs format."
  30.  
  31. 2)PRINT a polynomial P.
  32.  
  33. 3)EVALUATE a polynomial for the value x.
  34.  
  35. 4)ADD a polynomial P to a polynomial Q.
  36.  
  37. 5)SUBTRACT a polynomial P from a polynomial Q.
  38.  
  39. 6)MULTIPLY a polynomial P by a polynomial Q.
  40.  
  41. 7)COMPUTE the N-th power of a polynomial P.
  42.  
  43. 8)DIVIDE a polynomial P by a polynomial Q.  
  44. (2 3), (0 2), (0 1), (1 0).
  45.  
  46. 9)EXTRA CREDIT.  For extra credits, the following problems have to
  47. be performed by this program:
  48.  
  49. a.function(7)above is performed
  50. by this program.
  51.  
  52. b.the division of 2 specific polynomials is
  53. performed.  That is, given:
  54.  
  55. D = A + B
  56. E = A - B
  57.  
  58. Then, A=(3X^2 - 2X +1); B= (2 X^5 - 4X^3 +3)
  59.  
  60.        K= (3X^2 - 2X +1) + (2 X^5 - 4X^3 +3)
  61.           __________________________________
  62.            (3X^2 - 2X +1) - (2 X^5 - 4X^3 +3)
  63.  
  64.  
  65. c.J = (3X^2 - 2X +1)^3
  66.  
  67. It should be noted that each of the above functions is coded
  68. as a separate procedure within this program.
  69.  
  70. INPUT OR HOW PROGRAM IS EXECUTED?
  71.  
  72. The source of input is from a menu-driven system.  The test input for
  73. this program is from a test file.
  74.  
  75. The data file could be viewed as linked list. This program sets up
  76. and maintains the database. }
  77.  
  78. {********************************************************************}
  79. {File descriptions:1. hwfile is the input text file that
  80. should be viewed as  linked lists, that is delineated in the specifications.
  81.  
  82.  
  83. What follows are the global declarations for the main_program}
  84.  
  85.  
  86.  
  87. {********************************************************************}
  88.  
  89.  
  90. const
  91.   FileName = 'larry.dta';  {2nd programmer file containing input linked list}
  92.                            {Remember hwfile is output text file}
  93.  
  94.   Basex    = 'x';            {value of x variable assigned by Professor Yang
  95.                            in problem specifications}
  96.  
  97. type
  98.   PPolynomialNode = ^TPolynomialNode;
  99.   TPolynomialNode = record
  100.     Coefficient : integer;
  101.     Basex       : char;
  102.     Exponent    : integer;
  103.     Next  : pointer;  {Used to link each field}
  104.   end;
  105.  
  106.  
  107. {*************************************************************}
  108. procedure CreateNew(var Item: PPolynomialNode);
  109. begin
  110.  New(Item);
  111.   Item^.Next := nil;      {nil represents empty polynomial node before
  112.                            {polynomial processing}
  113.   Item^.Coefficient := 0;   {Polynomial coefficient}
  114.   Item^.Basex       := 'x';         {base x or value is assigned as x}
  115.   Item^.Exponent    := 0;       {exponent of polynomial read}
  116. end;
  117.     {This procedure initializes the linked list pointers to be used
  118.    on linked list file.}
  119.  
  120. {*************************************************************}
  121.  
  122. {The following procedure reads the linked list, and presents a user-friendly to the user
  123. menu to the user to enter their Coefficient, basex and exponent that
  124. relates to linked list.}
  125.  
  126. procedure GetData(var Item: PPolynomialNode);
  127. begin
  128.   ClrScr;
  129.    repeat
  130.     GotoXY(1, 1);
  131.     Write('Enter Coefficient: ');
  132.     Read(Item^.Coefficient);
  133.   until (Item^.Coefficient <> 99);
  134.   GotoXY(1, 2);
  135.   Write('Enter Base: ');
  136.   Writeln('Bases of -x- are only accepted');
  137.  
  138.   ReadLn(Item^.Basex);
  139.   if (item^.Basex) = '' then
  140.   begin
  141.   end;
  142.   GotoXY(1, 3);
  143.   Write('Enter Exponent: ');
  144.   ReadLn(Item^.Exponent);
  145. end;
  146. {************************************************************************}
  147.  
  148.  {The following computes the N-th power or factorial for the exponent
  149.  part of the polynomial and passes the result to the print function}
  150.  
  151.  function factorial (exponent: integer) :integer;
  152.           {factorial (exponent) computes n!}
  153.  
  154.  begin
  155.       if  exponent <= 1 then
  156.          factorial := 1
  157.       else
  158.           factorial := exponent * factorial(exponent - 1);
  159.           writeln('Professor Yang asked us to replace the value of');
  160.           writeln('the variable x with the value 3');
  161.           writeln ('This is the N-th power of a polynomial P');
  162.           write ('factorial', factorial(exponent));
  163.  end; { factorial}
  164.  
  165.  
  166. {*This procedure attempts to create and initialize the pointer, and assign
  167. head to null}
  168.  
  169. procedure DoFirst(var First, Current: PPolynomialNode);
  170. begin
  171.   CreateNew(Current);
  172.   GetData(Current);
  173.   First := Current;
  174. end;
  175. {***********************************************************************}
  176.  
  177. procedure Print(Head: PPolynomialNode);
  178. var
  179.   i: Integer;
  180.   Sum : Integer;
  181.   exponent : Integer;
  182.  
  183. begin
  184.   i := 1;
  185.   ClrScr;
  186.   while Head^.Next <> nil do begin
  187.     GotoXY(1, i); Write('Coefficient: ', Head^.Coefficient);  {position of coefficient on file}
  188.     GotoXY(25, i); Write('Basex: ', Head^.Basex); {position of base on file-spot 25}
  189.     GotoXY(45, i); Write('Exponent: ', Head^.Exponent);   {position of exponent on file}
  190.  
  191.     factorial(exponent);
  192.  
  193.     Sum := Head^.Coefficient * 3 * factorial(exponent); 
  194.  
  195.     {Professor requires that the basex of x should be replaced with the number,3}
  196.  
  197.     Head := Head^.Next;
  198.     Inc(i);
  199.   end;
  200.  
  201.      GotoXY(1, i);
  202.  
  203.  
  204.   ReadLn;
  205.  
  206. end;
  207.  {***********************************************************************}
  208.  
  209.  {The following procedure is used to add a polynomial Q to a polynomial P}
  210.  
  211.  procedure add_polynomial(var Item, Prev, Current: PPolynomialNode);
  212.  
  213.  {***********************************************************************}
  214. {The following procedure adds the linked list (P) with a polynomial
  215. Q}
  216.  
  217.  
  218. var
  219.  
  220. sum_coef : integer;
  221.  
  222. begin
  223.  
  224.   Prev := Current;
  225.   CreateNew(Current);
  226.   GetData(Current);
  227.  
  228.  If Item^.Exponent(Prev)  = Item^.Exponent(Current) then
  229.      sum_coef := Item^.Coefficient(Prev) + Item^.Exponent(Current);
  230.      Writeln(sum_coef);
  231.  
  232.   else
  233.  
  234.      Writeln(Item^.Coefficient(Prev));
  235.    
  236.      Writeln('This is the total of the previous and current polynomials');
  237.      Write(Item^.Basex(Prev));
  238.      Write(Item^.Exponent(Prev)); 
  239.  
  240.      Prev^.Next := Current;
  241.  
  242.  end; {of procedure add_polynomial}
  243.  
  244.   {***********************************************************************}
  245.  
  246.   {The following procedure is used to subtract a polynomial Q from a
  247.   polynomial Q}
  248.  
  249.    procedure subtract_polynomial(var Prev, Current: PPolynomialNode);
  250.  
  251.  
  252.    var
  253.  
  254.    dif_coef : integer;
  255.  
  256.  begin
  257.  
  258.   Prev := Current;
  259.   CreateNew(Current);
  260.   GetData(Current);
  261.  
  262.  If Item^.Exponent(Prev)  = Item^.Exponent(Current)
  263.      dif_coef =: Item^.Coefficient(Prev) - Item^.Exponent(Current);
  264.  
  265.  
  266.   Writeln('This is the difference between the previous and current');
  267.   Writeln(' polynomials');
  268.  
  269.   Writeln(dif_coef);
  270.   Write(Item^.Basex(Prev);
  271.   Write(Item^.Exponent(Prev);  }
  272.  
  273.   Prev^.Next := Current;
  274.  
  275.  
  276.  
  277.  end; {of procedure subtract_polynomial}
  278.   {***********************************************************************}
  279.  
  280.   {The following procedure is used to multiply a polynomial P from a
  281.   polynomial P}
  282.  
  283.  
  284.   procedure multiply_polynomial(var Prev, Current: PPolynomialNode);
  285.  
  286.  
  287.   var
  288.  
  289.   mcoef : integer;
  290.   sum_exp : integer;
  291.  
  292.  
  293.   begin
  294.    
  295.   Prev := Current;
  296.   CreateNew(Current);
  297.   GetData(Current);
  298.  
  299.  
  300.   mcoef   := Item^.Coefficient(Prev) * Item^.Coefficient(Current);
  301.   sum_exp := Item^.Exponent(Prev) + Item^.Exponent(Current);
  302.  
  303.   Writeln('This is the result of the multiplication of the previous');
  304.   Writeln(' and current polynomials');
  305.   Writeln(mcoef);
  306.   Write(Item.^Basex);
  307.   Write(sum_exp);
  308.                    
  309.   Prev^.Next := Current;
  310.  
  311.  
  312.   end; {of procedure multiply_polynomial}
  313.    {***********************************************************************}
  314.    {The following procedure is used to divide a polynomial P by a
  315.    polynomial P}
  316.  
  317.   procedure divide_polynomial(var Prev, Current: PPolynomialNode);
  318.  
  319.  
  320.   const
  321.  
  322.   dcoef = 0;       {used to initialize variable holding result of
  323.                      subtracting coefficients of 2 polynomials read
  324.                      by program}
  325.  
  326.   dexp  = 0;        { used to initialize variable holding result of
  327.                       subtracting exponents of 2 polynomials read
  328.                       by program}
  329.  
  330.  
  331.   var
  332.  
  333.   dcoef : integer;
  334.   dexp  : integer;
  335.  
  336.   begin
  337.  
  338.   Prev := Current;
  339.   CreateNew(Current);
  340.   GetData(Current);
  341.  
  342.   dcoef := Item^.Coefficient(Prev)/ Item^.Coefficient(Current);
  343.   dexp  := Item^.Exponent(Prev) / Item^.Exponent(Current);
  344.  
  345.   Writeln('This is the result of the division of the previous');
  346.   Writeln(' and current polynomials');
  347.  
  348.   Writeln('dcoef);
  349.   Write('Item^.Basex', Item^.Basex);
  350.   Write(dexp);   }
  351.  
  352.   Prev^.Next := Current;
  353.  
  354.  
  355.  
  356.   end; {of procedure divide_polynomial}
  357.   {***********************************************************************}
  358.  
  359.   {This part is for extra credit!}
  360.   procedure Extra_Credit(var Prev, Current: PPolynomialNode);
  361.  
  362.  
  363.   {Part 3 of the extra credit requirements involve 2 given polynomials.
  364.   Specifically, as follows:
  365.  
  366.   J = A^3
  367.  
  368.   K = D/E
  369.  
  370.   D = A + B
  371.   E = A - B
  372.  
  373.   A = 3x^2 - 2x +1; B = 2x^5 - 4x^3 +3}
  374.  
  375.   const
  376.  
  377.   3coef = 0;
  378.   3exp  = 0;
  379.   2coef = 0;
  380.   2exp  = 0;
  381.   1coef = 0;
  382.   1exp  = 0;
  383.  
  384.   DA+B_coef = 0;
  385.   DA-B_coef = 0;
  386.  
  387.  
  388.   type
  389.  
  390.   3coef : integer;
  391.   3exp  : integer;
  392.   2coef : integer;
  393.   2exp  : integer;
  394.   1coef : integer;
  395.   1exp  : integer;
  396.  
  397.   DA+B_coef : integer;
  398.   DA-B_coef : integer;
  399.  
  400.  
  401.   begin
  402.  
  403.   Prev := Current;
  404.   CreateNew(Current);
  405.   GetData(Current);
  406.   {*******************************************************************}
  407.   {This part of the extra credit is intended to divide 2 specific
  408.    polynomials.  These are:
  409.  
  410.    D = A + B;
  411.    E = A - B;
  412.  
  413.    K = D/E; }
  414.  
  415.  
  416.  
  417.   Writeln('This is the numerator of the extra credit portion');
  418.  
  419.   {Now, let's add the 2 polynomials together, and get a result}
  420.  
  421.   If Item^.Exponent(Prev)  = Item^.Exponent(Current)
  422.      DA+B_coef =: Item^.Coefficient(Prev) + Item^.Exponent(Current);
  423.  
  424.      Writeln('This is the total of the previous and current polynomials');
  425.      Writeln(DA+B_coef);
  426.  
  427.      Numerator_Basex    := Item^.Basex(Prev);
  428.      Numerator_Exponent := Item^.Exponent(Prev);
  429.  
  430.   Else
  431.         Numerator_Coefficient := Item^.Coefficient(Prev);
  432.         Numerator_Basex       := Item^.Basex(Prev);
  433.         Numerator_Exponent    := Item^.Exponent(Prev);
  434.  
  435.   
  436.         Writeln(Numerator_Coefficient);
  437.         Write(Numerator_Basex);
  438.         Write(Numerator_Exponent);
  439.  
  440.         Writeln(Item^.Coefficient(Current);
  441.         Write(Item^.Basex(Current);
  442.         Write(Item^.Exponent(Current);
  443.  
  444.   Prev^.Next := Current;
  445.  
  446.   {Now, to compute the divisor}
  447.  
  448.    If Item^.Exponent(Prev)  = Item^.Exponent(Current)
  449.        DA-B_coef =: Item^.Coefficient(Prev) - Item^.Exponent(Current);
  450.  
  451.        Divisor_Basex    := Item^.Basex(Prev);
  452.        Divisor_Exponent := Item^.Exponent(Prev);
  453.  
  454.        Writeln(DA-B_coef);
  455.        Write(Divisor_Basex);
  456.        Write(Divisor_Exponent);
  457.  
  458.    Else
  459.  
  460.        Writeln(Item^.Coefficient(Prev);
  461.        Write(Item^.Basex(Prev);
  462.        Write(Item^.Exponent(Prev);
  463.  
  464.        Writeln(Item^.Coefficient(Current);
  465.        Write(Item^.Basex(Current);
  466.        Write(Item^.Exponent(Current);
  467.  
  468.  
  469.   Prev^.Next := Current;
  470.  
  471.  Writeln('Now, to display the anwer of K = D/E');
  472.  
  473.  
  474.  Writeln((Numerator_Coefficient + Numerator_Basex + Numerator_Exponent +
  475.   Coefficient(Prev) + Item^.Basex(Prev) + Item^.Exponent(Prev) +
  476.   Item^.Coefficient(Current) + Item^.Basex(Current) + Item^.Exponent(Current))
  477.  
  478.   /
  479.  
  480.  
  481.  (Divisor_Coefficient + Divisor_Basex + Divisor_Exponent +
  482.   Coefficient(Prev) + Item^.Basex(Prev) + Item^.Exponent(Previous) +
  483.   Item^.Coefficient(Current) + Item^.Basex(Current) + Item^.Exponent(Current)))
  484.  
  485.  
  486.   {****************************************************************}
  487.   { This part of the extra credit computes the following:
  488.     (3x^2 - 2x +1)^3}
  489.  
  490.  
  491.   If Item^.Coefficient(prev) = 3 then
  492.  
  493.          3coef =: Item^.Coefficient(Prev) *3;
  494.          3exp  =: Item^.Exponent(Prev)    *3;
  495.  
  496.   If  Item^.Coefficient(prev) = 2 then
  497.  
  498.          2coef =: Item^.Coefficient(Prev) *3;
  499.          2exp  =: Item^.Exponent(Prev)    *3;
  500.  
  501.   If  Item^.Coefficient(prev) = 1 then
  502.  
  503.          1coef =: Item^.Coefficient(Prev) *3;
  504.          1exp  =: Item^.Exponent(Prev)    *3;
  505.  
  506.   Writeln(3coef);
  507.   Write(3exp);
  508.   Write(2coef);
  509.   Write(2exp);
  510.   Write(1coef);
  511.   Write(1exp);
  512.  
  513.   end
  514.  
  515.  
  516.   {***********************************************************************}
  517.  
  518.  {The following procedure is necessary since it disposes of the Previously
  519.  assigned pointer, and frees the dynamically-assigned memory.}
  520.  
  521. procedure FreeAll(var Head: PPolynomialNode);
  522. var
  523.   Temp: PPolynomialNode;
  524. begin
  525.   while Head^.Next <> nil do begin
  526.     Temp := Head^.Next;
  527.     Dispose(Head);
  528.     Head := Temp;
  529.   end;
  530.   Dispose(Head);
  531. end;
  532. {***********************************************************************}
  533.  
  534. procedure CreateNewFile(Head: PPolynomialNode);
  535. var
  536.   F: File of TPolynomialNode;
  537. begin
  538.   Assign(F, FileName);
  539.   ReWrite(F);
  540.   while Head^.Next <> nil do begin
  541.     Write(F, Head^);
  542.     Head := Head^.Next;
  543.   end;
  544.   Write(F, Head^);
  545.   Close(F);
  546. end;
  547. {*****************************************************************}
  548. procedure ReadFile(var First, Prev, Current: PPolynomialNode);
  549. var
  550.   F: File of TPolynomialNode;
  551. begin
  552.   Assign(F, FileName);  {assigns the filename to a file variable}
  553.   Reset(F);             {command necessary to open file for readig}
  554.   CreateNew(Current);   {creates new pointer}
  555.   Read(F, Current^);    {reads first record}
  556.   First := Current;     {if first and current record same, then user
  557.                         {at start of linked list file processing}
  558.   while not Eof(F) do begin
  559.     Prev := Current;
  560.     CreateNew(Current);
  561.     Read(F, Current^);
  562.     Prev^.Next := Current;
  563.   end;
  564.   Close(F);              {if user at end of file, then file is closed
  565.                          and linked list comprising passenger name,
  566.                          flight number and day of week }
  567. end;
  568. {*****************************************************************}
  569.  
  570. {This procedure is the main processing for Larry Marks program. }
  571.  
  572. procedure Main(var First, Prev, Current: PPolynomialNode);
  573. var
  574.   F      : Text;
  575. begin
  576.   {$I-}
  577.   Assign (f, 'pr1file.txt'); {this file is used to contain test linked list
  578.                              of polynomials}
  579.  
  580.   Reset(f);
  581.   {$I+}                           {standard prog of Turbo Pascal}
  582.   if (IOResult <> 0) then begin
  583.     WriteLn('error Reading File');{I want to test file to see if opened}
  584.     Halt;                         {if already opened, then file processing ends.}
  585.   end;
  586.   CreateNew(Current);
  587.   ReadLn(F, Current^.Coefficient);
  588.   ReadLn(F, Current^.Basex);
  589.   ReadLn(F, Current^.Exponent);
  590.   First := Current;
  591.   while not Eof(F) do begin
  592.     Prev := Current;
  593.     CreateNew(Current);
  594.     ReadLn(F, Current^.Coefficient);
  595.     ReadLn(F, Current^.Basex);
  596.     ReadLn(F, Current^.Exponent);
  597.  
  598.     Add_Polynomial(First, Prev, Current);
  599.     Subtract_Polynomial(Prev, Current);
  600.     Multiply_Polynomial(Prev, Current);
  601.     Divide_Polynomial(Prev, Current);
  602.  
  603.  
  604.     Extra_Credit(Prev, Current);
  605.     
  606.     Prev^.Next := Current;
  607.   end;
  608.   Close(F);
  609.   Print(First);
  610.   CreateNewFile(First);
  611. end;
  612. {**************************************************************}
  613. {I decided to change the specifications somewhat in order to present}
  614. {to the user a friendly modularized menu which contains the system}
  615. function WriteMenu: Char;
  616. var
  617.   Ch: Char;
  618. begin                                {main menu system}
  619.   ClrScr;
  620.   GotoXY(1, 1);
  621.           writeln('*********************************************');
  622.           writeln('*********************************************');
  623.           writeln('Prof. Oscar Yang super duper polynomial system');
  624.           writeln('*********************************************');
  625.           writeln('*********************************************');
  626.           writeln('linked list not exist at this time'          );
  627.           writeln('Please wait while Professor Hao sets up list');
  628.           writeln('node initializing next'                      );
  629.           writeln('*************************************');
  630.           writeln('Now, passenger loop processing begins');
  631.           writeln('*************************************');
  632.  
  633.   WriteLn('A) Add');
  634.   WriteLn('P) Print');
  635.   Writeln('S) Subtract');
  636.   Writeln('M) Multiply');
  637.   Writeln('D) Divide');
  638.   
  639.   WriteLn('E) Extra Credit');
  640.  
  641.   WriteLn('X) Exit');
  642.   repeat
  643.     Ch := UpCase(ReadKey);    {this validation checking checks lower case,
  644.     and changes all letters entered to upper case.}
  645.  
  646.   until Ch in ['A', 'P', 'S', 'M', 'D', 'W', 'X'];
  647.   WriteMenu := Ch;
  648. end;
  649.  
  650. var
  651.   Ch: Char;
  652.   First,
  653.   Prev,
  654.   Current: PPolynomialNode;
  655.  
  656. begin
  657.   ClrScr;
  658.   {  Main(First, Prev, Current); Use this option to read text file }
  659.   ReadFile(First, Prev, Current);
  660.   repeat
  661.     Ch := WriteMenu;
  662.     case Ch of
  663.       'A': Add_Polynomial(First, Prev, Current);
  664.       'P': Print(First);
  665.       'W': CreateNewFile(First);
  666.       'S': Subtract_Polynomial(Prev, Current);
  667.       'M': Multiply_Polynomial(Prev, Current);
  668.       'E': Extra_Credit(Prev, Current);
  669.       'D': Divide_Polynomial(Prev, Current);
  670.     end;
  671.   until Ch = 'X';
  672. end.
  673. end. { main program}
  674.  
  675.